/////read in counterfactual simulated data

**no behavioreal resopnse
import delimited "$dir/Model/Simulated_data/simulated_data_cfact_movecosts_nob.csv", clear
gen uniqid = _n
keep uniqid v26 v14 v24 v22
ren v26 pctile_nob
ren v24 faminc_nob
ren v22 util_nob
tempfile nob
save `nob'

**full behavioral response
import delimited "$dir/Model/Simulated_data/simulated_data_cfact_movecosts.csv", clear
gen uniqid = _n
keep uniqid v11 v12 v9
ren v12 pctile_movecost
ren v11 faminc_movecost
ren v9 util_movecost
su util_movecost, d
replace util_movecost = `r(p1)' if util_movecost<`r(p1)'
replace util_movecost = `r(p99)' if util_movecost>`r(p99)'
tempfile movecost
save `movecost'

**decomp 1
import delimited "$dir/Model/Simulated_data/simulated_data_cfact_movecosts_decomp_1.csv", clear
gen uniqid = _n
keep uniqid v21 v14 v20
ren v21 pctile_decomp_1
ren v20 faminc_decomp_1
tempfile decomp_1
save `decomp_1'

**decomp 2
import delimited "$dir/Model/Simulated_data/simulated_data_cfact_movecosts_decomp_2.csv", clear
gen uniqid = _n
keep uniqid v21 v14 v20
ren v21 pctile_decomp_2
ren v20 faminc_decomp_2
tempfile decomp_2
save `decomp_2'

//baseline
import delimited "$dir/Model/Simulated_data/simulated_data_base.csv", clear
gen uniqid = _n

keep uniqid v1 v25 v26 v14 v24 v22
ren v1 statefips
ren v26 pctile
ren v24 faminc
keep if v25<50
ren v22 util

merge 1:1 uniqid using `nob', keep(match) nogen
merge 1:1 uniqid using `movecost', keep(match) nogen
merge 1:1 uniqid using `decomp_1', keep(match) nogen
merge 1:1 uniqid using `decomp_2', keep(match) nogen 





ren v14 weight
collapse (mean) util* pctile* faminc* weight, by(statefips)
save "$temp/cfact_move_data", replace



use "$temp/cfact_move_data", clear
gen diff_nob = pctile_nob - pctile
gen diff_movecost = pctile_movecost-pctile
gen diff_decomp_1 = pctile_decomp_1 - pctile
gen diff_decomp_2 = pctile_decomp_2 - pctile


gen util_diff_nob = util_nob - util
gen util_diff_movecost = util_movecost - util


gen inc_diff_nob = faminc_nob - faminc
gen inc_diff_movecost = faminc_movecost - faminc
gen inc_diff_decomp_1 = faminc_decomp_1 - faminc
gen inc_diff_decomp_2 = faminc_decomp_2 - faminc

ren statefips stfips
merge 1:m stfips using "$data/Crosswalks/state_div_crosswalk", keep(match) nogen
gen gooddiv = (div == 4 | div == 8)

//here we go
local vars `"pctile diff_nob diff_movecost diff_decomp_1 diff_decomp_2"'
foreach var in `vars'{
    qui su `var'  if div == 4 | div == 8
	local r1 = round(`r(mean)', 0.001)
	local s1 = round(`r(sd)'/sqrt(15), 0.001)
	qui su `var'  if div != 4 & div != 8
	local r2 = round(`r(mean)', 0.001)
	local s2 = round(`r(sd)'/sqrt(35), 0.001)
	
	
	local diff = round(`r1' - `r2', 0.001)
	qui ttest `var', by(gooddiv)
	local t = round(`r(se)', 0.01)
	
	
	noi di "`var': `r1' || `s1' ||  `r2' || `s2' || `diff' ||  `t'"
}


gen statefips = stfips
merge 1:1 statefips using "$temp/eval_data", keep(match) nogen
 
 //faminc inc_diff_nob inc_diff_movecost inc_diff_decomp_1 inc_diff_decomp_2
 
 
//scatterplots
//ren stfips statefips
//merge m:1 statefips using "$temp/simulated_model_baseline", keep(match) nogen
scatter diff_nob iim || lfit diff_nob iim, bgcolor(white) graphregion(color(white)) xtitle("IIM (Data)") ytitle("Change in IIM, No BR") ylabel(-12(2)4) legend(off) text(-8 50  ///
"{&beta}: -.109" ///
"      (0.076)" ///
"R{superscript:2}: 0.04" ///
)
graph export "$output/fig6_cfact_iim_nomove_scatter_nobr.png", replace


scatter diff_movecost iim || lfit diff_movecost iim, bgcolor(white) graphregion(color(white)) xtitle("IIM (Data)") ytitle("Change in IIM, Full BR") ylabel(-12(2)4) legend(off) text(0 50  ///
"{&beta}: -.188" ///
"      (0.106)" ///
"R{superscript:2}: 0.06" ///
)

graph export "$output/fig6_cfact_iim_nomove_scatter_br.png", replace 
 
 
 
maptile diff_movecost, geo(state) cutv(-6.0 -4.5 -3.0 -1.5 0.0 1.5) rev spopt(mos(thick)) legdecimals(1) 
graph export "$output/fig6_cfact_movecosts.png", replace

maptile diff_nob, geo(state) cutv(-6.0 -4.5 -3.0 -1.5 0.0 1.5) rev spopt(mos(thick)legenda(off)) legdecimals(1) 
graph export "$output/fig6_cfact_movecosts_nob.png", replace
graph close

maptile util_diff_movecost, geo(state) n(9) rev spopt(mos(thick)) legdecimals(1) 
graph export "$output/fig6_cfact_movecosts_util.png", replace
graph close

exit
 
